﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Domain
{
	using System.Linq;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Extensions;
	using Falcon.Forums.Domain.Enums;

	public partial class Forum
	{
		/// <summary>
		/// Determines whether this instance [can view forum] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can view forum] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanViewForum
		{
			get
			{
				return GetResult(ForumPermissionActions.View);
			}
		}

		/// <summary>
		/// Determines whether this instance [can search forum] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can search forum] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanSearchForum
		{
			get
			{
				return GetResult(ForumPermissionActions.View);
			}
		}

		/// <summary>
		/// Determines whether this instance [can view post] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can view post] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanViewPost
		{
			get
			{
				return GetResult(ForumPostPermissionActions.View);
			}
		}

		/// <summary>
		/// Determines whether this instance [can view any post] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can view any post] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanViewAnyPost
		{
			get
			{
				return GetResult(ForumPostPermissionActions.ViewAny);
			}
		}

		/// <summary>
		/// Determines whether this instance [can view attachment] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can view attachment] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanViewAttachment
		{
			get
			{
				return GetResult(ForumPostPermissionActions.ViewAttachment);
			}
		}

		/// <summary>
		/// Determines whether this instance [can create post] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can create post] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanCreatePost
		{
			get
			{
				return GetResult(ForumPostPermissionActions.Create);
			}
		}

		/// <summary>
		/// Determines whether this instance [can create post attachment] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can create post attachment] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanCreatePostAttachment
		{
			get
			{
				return GetResult(ForumPostPermissionActions.CreateAttachment);
			}
		}

		/// <summary>
		/// Determines whether this instance [can update post] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can update post] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanUpdatePost
		{
			get
			{
				return GetResult(ForumPostPermissionActions.Update);
			}
		}

		/// <summary>
		/// Determines whether this instance [can update any post] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can update any post] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanUpdateAnyPost
		{
			get
			{
				return GetResult(ForumPostPermissionActions.UpdateAny);
			}
		}

		/// <summary>
		/// Determines whether this instance [can delete post] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can delete post] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanDeletePost
		{
			get
			{
				return GetResult(ForumPostPermissionActions.Delete);
			}
		}

		/// <summary>
		/// Determines whether this instance [can delete any post] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can delete any post] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanDeleteAnyPost
		{
			get
			{
				return GetResult(ForumPostPermissionActions.DeleteAny);
			}
		}

		/// <summary>
		/// Gets a value indicating whether this instance can move any post.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance can move any post; otherwise, <c>false</c>.
		/// </value>
		public virtual bool CanMoveAnyPost
		{
			get
			{
				return GetResult(ForumPostPermissionActions.MoveAny);
			}
		}

		/// <summary>
		/// Determines whether this instance [can delete post attachment] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can delete post attachment] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanDeletePostAttachment
		{
			get
			{
				return GetResult(ForumPostPermissionActions.DeleteAttachment);
			}
		}

		/// <summary>
		/// Determines whether this instance [can delete any post attachment] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can delete any post attachment] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanDeleteAnyPostAttachment
		{
			get
			{
				return GetResult(ForumPostPermissionActions.DeleteAnyAttachment);
			}
		}

		/// <summary>
		/// Determines whether this instance [can view topic] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can view topic] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanViewTopic
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.View);
			}
		}

		/// <summary>
		/// Determines whether this instance [can view any topic] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can view any topic] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanViewAnyTopic
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.ViewAny);
			}
		}

		/// <summary>
		/// Determines whether this instance [can create topic] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can create topic] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanCreateTopic
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.Create);
			}
		}

		/// <summary>
		/// Determines whether this instance [can create topic poll] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can create topic poll] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanCreateTopicPoll
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.CreatePoll);
			}
		}

		/// <summary>
		/// Determines whether this instance [can update topic] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can update topic] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanUpdateTopic
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.Update);
			}
		}

		/// <summary>
		/// Determines whether this instance [can update any topic] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can update any topic] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanUpdateAnyTopic
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.UpdateAny);
			}
		}

		/// <summary>
		/// Determines whether this instance [can update topic poll] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can update topic poll] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanUpdateTopicPoll
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.UpdatePoll);
			}
		}

		/// <summary>
		/// Determines whether this instance [can update any topic poll] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can update any topic poll] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanUpdateAnyTopicPoll
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.UpdateAnyPoll);
			}
		}

		/// <summary>
		/// Determines whether this instance [can delete topic] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can delete topic] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanDeleteTopic
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.Delete);
			}
		}

		/// <summary>
		/// Determines whether this instance [can delete any topic] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can delete any topic] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanDeleteAnyTopic
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.Delete);
			}
		}

		/// <summary>
		/// Determines whether this instance [can move topic] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can move topic] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanMoveTopic
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.Move);
			}
		}

		/// <summary>
		/// Determines whether this instance [can move any topic] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can move any topic] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanMoveAnyTopic
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.MoveAny);
			}
		}

		/// <summary>
		/// Determines whether this instance [can close topic] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can close topic] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanCloseTopic
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.Close);
			}
		}

		/// <summary>
		/// Determines whether this instance [can close any topic] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can close any topic] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanCloseAnyTopic
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.CloseAny);
			}
		}

		/// <summary>
		/// Determines whether this instance [can tag topic] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can tag topic] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanTagTopic
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.Tag);
			}
		}

		/// <summary>
		/// Determines whether this instance [can tag any topic] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can tag any topic] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanTagAnyTopic
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.TagAny);
			}
		}

		/// <summary>
		/// Determines whether this instance [can vote on topic poll] the specified permissions.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if this instance [can vote on topic poll] the specified permissions; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool CanVoteOnTopicPoll
		{
			get
			{
				return GetResult(ForumTopicPermissionActions.VotePoll);
			}
		}

		/// <summary>
		/// Gets the result whether the specified permissions has the specified actions.
		/// </summary>
		/// <param name="actions">The actions.</param>
		/// <returns></returns>
		private bool GetResult(ForumPermissionActions actions)
		{
			return this.Permissions.HasValue() && this.Permissions.Any(x => Has(x.Role.Title, UserPermissionActions.View) && x.Action.Has(actions));
		}

		/// <summary>
		/// Gets the result whether the specified permissions has the specified actions.
		/// </summary>
		/// <param name="actions">The actions.</param>
		/// <returns></returns>
		private bool GetResult(ForumPostPermissionActions actions)
		{
			return this.Permissions.HasValue() && this.Permissions.Any(x => Has(x.Role.Title, UserPermissionActions.View) && x.PostAction.Has(actions));
		}

		/// <summary>
		/// Gets the result whether the specified permissions has the specified actions.
		/// </summary>
		/// <param name="actions">The actions.</param>
		/// <returns></returns>
		private bool GetResult(ForumTopicPermissionActions actions)
		{
			return this.Permissions.HasValue() && this.Permissions.Any(x => Has(x.Role.Title, UserPermissionActions.View) && x.TopicAction.Has(actions));
		}
	}
}